<template>
    <!-- 课程标签管理 -->
    <div class="scheme">
        <div class="header-box">

            <el-button type="success" icon="el-icon-plus" @click="add">新增</el-button>
        </div>
        <div class="tables">
            <el-table border :data="tableData" max-height="650" highlight-current-row :row-style="{height: '20px'}"
                      :header-cell-style="{background:'#e6e6e6',color:'#000000'}">
                <el-table-column prop="id" align="center" label="ID" width="60">
                </el-table-column>
                <el-table-column prop="name" align="center" label="名称">
                </el-table-column>
                <el-table-column prop="code" align="center" label="code"></el-table-column>
                <el-table-column prop="order" align="center" label="排序"></el-table-column>
                <el-table-column prop="lay_out" align="center" label="布局">
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.lay_out === 1 ? 'success' : 'danger'"
                                disable-transitions>{{ scope.row.lay_out === 1 ? '左右' : '上下' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="status" align="center" label="状态">
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'"
                                disable-transitions>{{ scope.row.status === 1 ? '启用' : '禁用' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="created_at" align="center" label="创建时间">
                </el-table-column>
                <el-table-column prop="updated_at" align="center" label="更新时间">
                </el-table-column>
                <el-table-column align="center" label="操作" width="250">
                    <template slot-scope="scope">
                        <el-button size="mini" type="success" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑
                        </el-button>
                        <el-button size="mini" type="danger" plain icon="el-icon-delete"
                                   @click="handleDelete(scope.row)">删除
                        </el-button>
                    </template>

                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                               :current-page.sync="page" :page-sizes="[15, 20, 50, 100]" :page-size="limit"
                               layout="total, sizes, prev, pager, next, jumper" :total="total">
                </el-pagination>
            </div>
        </div>

        <!-- 新增/编辑弹框 -->
        <el-dialog :title="title" :visible.sync="dialogAdd" width="40%">
            <el-form :model="form" label-width="80px">
                <el-form-item label="名称:" style="width: 60%">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="code:" style="width: 60%">
                    <el-input v-model="form.code" type="number"></el-input>
                </el-form-item>
                <el-form-item label="布局" prop="lay_out">
                    <el-radio-group v-model="form.lay_out" size="medium">
                        <el-radio border label="1">左右</el-radio>
                        <el-radio border label="2">上下</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="排序" prop="order">
                    <el-input-number v-model="form.order" :min="1" :max="10000"></el-input-number>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="form.status" size="medium">
                        <el-radio border label="1">启用</el-radio>
                        <el-radio border label="2">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogAdd = false">取 消</el-button>
                <el-button type="primary" @click="submitDialog">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
export default {
    name: "course_tag",
    data() {
        return {
            form: {
                name: "",
                code: "",
                lay_out: "",
                status: "",
                order: "",
            },
            tableData: [],
            dialogAdd: false, //默认新增课程弹框隐藏
            title: "新增", //默认新增
            isEdit: false, //默认为新增
            id: "",
            page: 1, //默认第一页
            limit: 15, //默认一页显示10条数据
            total: 0,
        };
    },
    created() {
        this.getCourseTagList();
    },
    activated() {
        this.getCourseTagList();
    },
    methods: {
        initForm() {
            this.form.name = "";
            this.form.code = "";
            this.form.lay_out = "";
            this.form.status = "";
            this.form.order = "";

        },
        // 点击新增课程按钮
        add() {
            this.title = "新增标识";
            this.initForm();
            this.dialogAdd = true;
            this.isEdit = false;
        },


        // 获取列表
        getCourseTagList(page, limit) {
            let params = {
                page: page,
                per_page: limit
            };
            this.$get(this.$api.courseTagApi, params).then((res) => {
                if (res.code == 200) {
                    this.tableData = res.data.data;
                    this.total = res.data.total;
                } else {
                    return this.$message.error(res.msg);
                }
            });
        },

        // 点击编辑按钮
        handleEdit(row) {
            this.title = "编辑标识";
            this.id = row.id;
            this.form.name = row.name;
            this.form.code = row.code;
            this.form.lay_out = row.lay_out.toString();
            this.form.status = row.status.toString();
            this.form.order = row.order;
            this.dialogAdd = true;
            this.isEdit = true;
        },
        // 点击删除按钮
        handleDelete(row) {
            this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    this.$delete(this.$api.courseTagApi + "/" + row.id).then((res) => {
                        if (res.code == 200) {
                            this.getCourseTagList(this.page, this.limit);
                            return this.$message.success("删除成功！");
                        } else {
                            return this.$message.error(res.msg);
                        }
                    });
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除",
                    });
                });
        },
        // 分页
        handleSizeChange(size) {
            this.limit = size;
            this.getCourseTagList(this.page, this.limit);
        },
        handleCurrentChange(index) {
            this.page = index;
            this.getCourseTagList(this.page, this.limit);
        },
        // 点击查询按钮
        submitForm() {
            this.getCourseTagList(this.page, this.limit);
        },

        // 点击新增课程确定按钮
        submitDialog() {
            if (this.form.name == "") {
                return this.$message.error("请输入名称！");
            }
            if (this.form.code == "") {
                return this.$message.error("请输入code！");
            }
            if (this.form.lay_out == "") {
                return this.$message.error("请选择布局！");
            }
            if (this.form.status == "") {
                return this.$message.error("请选择状态！");
            }
            if (this.form.order == "") {
                return this.$message.error("请输入排序！");
            }
            let params = {
                name: this.form.name,
                code: this.form.code,
                lay_out: this.form.lay_out,
                status: this.form.status,
                order: this.form.order,
            };
            if (this.isEdit) {
                this.$put(this.$api.courseTagApi + "/" + this.id, params).then(
                    (res) => {
                        if (res.code == 200) {
                            this.form.name = "";
                            this.form.status = 2;
                            this.dialogAdd = false;
                            this.getCourseTagList(this.page, this.limit);
                            return this.$message.success("编辑成功！");
                        } else {
                            return this.$message.error(res.msg);
                        }
                    }
                );

            } else {
                this.$post(this.$api.courseTagApi, params).then((res) => {
                    if (res.code == 200) {
                        this.dialogAdd = false;
                        this.getCourseTagList(this.page, this.limit);
                        return this.$message.success("新增成功！");
                    } else {
                        return this.$message.error(res.msg);
                    }
                });

            }
        },
    },
};
</script>

<style scoped>
.pagination {
    text-align: center;
}

</style>
